package com.jxtc.enterprise.hr.controller;

import com.jxtc.enterprise.hr.dto.DepartmentDTO;
import com.jxtc.enterprise.common.utils.Result;
import com.jxtc.enterprise.common.utils.ResultGenerator;
import com.jxtc.enterprise.hr.dto.DepartmentUpdateDTO;
import com.jxtc.enterprise.hr.service.HrDepartmentManagementService;
import com.jxtc.enterprise.hr.vo.DepartmentVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/jxtc/enterprise/department")
@RequiredArgsConstructor
public class DepartmentManagementController {

    private final HrDepartmentManagementService departmentManagementService;

    @ApiResponse(responseCode = "200", description = "code 为 200 时，data 字段返回部门信息列表")
    @ApiResponse(responseCode = "400", description = "code 为 400 时，message 字段返回错误提示信息")
    @Parameter(name = "companyId", description = "公司 ID", required = true, example = "c752804d0cd410267f404dd3f0457a57")
    @Operation(summary = "7.1 获取公司下所有部门", tags = "7. 部门管理")
    @GetMapping
    public Result<List<DepartmentVO>> listDepartmentByCompanyId(@RequestParam String companyId) {
        List<DepartmentVO> voList = departmentManagementService.listDepartmentByCompanyId(companyId);
        return ResultGenerator.genSuccessResult(voList);
    }

    @ApiResponse(responseCode = "200", description = "code 为 200 时， 表示新增部门成功，此时 data 字段为空")
    @ApiResponse(responseCode = "400", description = "code 为 400 时， 表示新增部门失败，此时 message 字段给出错误信息")
    @Operation(summary = "7.2 为公司添加部门", tags = "7. 部门管理", operationId = "7.2")
    @PostMapping
    public Result<DepartmentVO> addDepartment(@Valid @RequestBody DepartmentDTO dto) {
        DepartmentVO vo = departmentManagementService.saveDepartment(dto);
        return ResultGenerator.genSuccessResult(vo);
    }

    @ApiResponse(responseCode = "200", description = "code 为 200 时， 表示修改部门信息成功，此时 data 字段为空")
    @ApiResponse(responseCode = "400", description = "code 为 400 时， 表示修改部门信息失败，此时 message 字段给出错误信息")
    @Operation(summary = "7.3 修改部门信息", tags = "7. 部门管理", operationId = "7.3")
    @PutMapping
    public Result<Void> updateDepartment(@Valid @RequestBody DepartmentUpdateDTO dto) {
        departmentManagementService.updateDepartmentById(dto);
        return ResultGenerator.genSuccessResult();
    }

    @ApiResponse(responseCode = "200", description = "code 为 200 时， 表示删除部门信息成功，此时 data 字段为空")
    @ApiResponse(responseCode = "400", description = "code 为 400 时， 表示删除部门信息失败，此时 message 字段给出错误信息")
    @Parameter(name = "id", description = "部门 ID", example = "6abbbaddd960ba64e9af1339317ca21d")
    @Operation(summary = "7.4 删除部门信息", tags = "7. 部门管理", operationId = "7.4")
    @DeleteMapping
    public Result<Void> deleteDepartment(@RequestParam String id) {
        departmentManagementService.deleteDepartmentById(id);
        return ResultGenerator.genSuccessResult();
    }
}
